/*
 * enums.h
 *
 *  Created on: Dec 11, 2011
 *      Author: Michael Anderson
 *      Part of Team 955 Battery Cart project
 *      compiled on Eclipse with Arduino libraries and avr-gcc
 */

#ifndef ENUMS_H_
#define ENUMS_H_

#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

#define NUM_CHARGING_CIRCUITS 4
#define NUM_CONDITIONERS 2
#define NUM_DISPLAYS 1
#define NUM_LOADS 3
#define NUM_COND_STAGES 6 //Add one for the "no load" stage
#define SERIAL_SPEED 115200

//ANALOG READ PARAMETERS
//This speeds up analogRead dramatically, at the price of some precision.
//However, it's worth the tradeoff in this case.
#define FASTADC 1
#if FASTADC
#define ANALOG_READ_MILLIS 0.016
#else
#define ANALOG_READ_MILLIS 0.11
#endif
#define ANALOG_READ_DELAY 4
#define POWER_WAVEFORM_LENGTH_MILLIS 8.8
#define ANALOG_READ_SAMPLE_LEN (POWER_WAVEFORM_LENGTH_MILLIS / ANALOG_READ_MILLIS)

//DISPLAY
#define MAX_TICKER_LEN 128
#define DISPLAY_COLS 20
#define DISPLAY_ROWS 4
#define TICKER_SPACING 2
#define TICKER_DELAY 300

//QUEUE
#define QUEUE_MAX_DEPTH 3

//RESULTS MENU
#define RESULTS_MENU_NUM_PAGES 3
#define WATTHOUR_MENU_DATA_COLS 2
#define WATTHOUR_MENU_PRECISION 2
#define WATTHOUR_MENU_ENTRY_SPACING 2
#define WATTHOUR_MENU_REFRESH_DELAY 500

//DEBUG MENU
#define DEBUG_MENU_NUM_PAGES 6
#define DEBUG_MENU_REFRESH_DELAY 500

//CHARGER
#define CHARGER_TIMEOUT_MINS 60 * 12 //12 hours for now

//CONDITIONER
#define INVERTER_CHECK_DELAY 3500
//The analog current reading threshold below which the inverter is judged to have turned off
#define INVERTER_VOLTAGE_THRESHOLD 10.2
#define COND_CURRENT_GAIN 2.
#define COND_VOLTAGE_GAIN 2.5
#define COND_CURRENT_REF 2.5
#define COND_VOLTAGE_REF 10.
#define ANALOG_MAX_VALUE 1023.
#define ANALOG_MAX_VOLTAGE 5.
#define COND_CURRENT_OPAMP_RANGE 2.
#define COND_VOLTAGE_RANGE 2.5
#define COND_MAX_AMPS 50.
#define COND_MIN_VOLTS 10.5
#define COND_CUTOFF_AMPS 1.
#define COND_CUTOFF_VOLTS 10.2
#define COND_MIN_RAW_V (int)((COND_CUTOFF_VOLTS - COND_VOLTAGE_REF) / COND_VOLTAGE_RANGE * ANALOG_MAX_VALUE)
#define COND_MIN_RAW_I (int)((COND_CUTOFF_AMPS / COND_MAX_AMPS) * ANALOG_MAX_VALUE)

//TEST
#define MILLIS_PER_MINUTE 60000
#define TEST_WAIT_MINUTES 5
#define TEST_WAIT_MILLIS TEST_WAIT_MINUTES * MILLIS_PER_MINUTE
#define BATTERY_TEST_MINUTES_TO_WATT_HOURS 300. / 60. * 1.1
#define PULSE_TEST_LENGTH_SECONDS 20.
#define BATTERY_TEST_VOLTAGE_THRESHOLD 11.2
#define BATTERY_TEST_CURRENT_THRESHOLD 1.
#define BATTERY_TEST_RAW_VOLTAGE_THRESHOLD (int)((BATTERY_TEST_VOLTAGE_THRESHOLD - COND_VOLTAGE_REF) / COND_VOLTAGE_RANGE * ANALOG_MAX_VALUE)
#define BATTERY_TEST_RAW_CURRENT_THRESHOLD (int)((BATTERY_TEST_CURRENT_THRESHOLD / COND_MAX_AMPS) * ANALOG_MAX_VALUE)

//SERIAL
#define ENABLE_SERIAL_DEBUG 0
#define ENABLE_SERIAL_MENUS 1
#define ENABLE_DEBUG_MENU 1

#if ENABLE_SERIAL_DEBUG
#define debugln( x ) Serial.println( x )
#else
#define debugln( x )
#endif

//First dimension is one of the load configurations, second dimension is on/off values for each load
const int conditioner_loads[NUM_COND_STAGES][NUM_LOADS] = {
        { 0, 0, 0 },
        { 1, 0, 0 },
        { 0, 1, 0 },
        { 1, 1, 0 },
        { 0, 0, 1 },
        { 0, 1, 1 }
};

enum menu_action {
    MENU_ACTION_NONE = 0,
    MENU_ACTION_CANCEL_PROCESS,
    MENU_ACTION_CLEAR_QUEUE,
    MENU_ACTION_CLEAR_RESULTS
};

enum queue_action {
    QUEUE_ACTION_NONE = 0,
    QUEUE_ACTION_CHARGE,
    QUEUE_ACTION_CONDITION_PULSE,
    QUEUE_ACTION_CONDITION_LOW,
    QUEUE_ACTION_TEST_QUICK,
    QUEUE_ACTION_TEST_HIGH_RATE
};

enum battery_state {
    BATTERY_IDLE = 0,
    BATTERY_CONNECTED_TO_CHARGER,
    BATTERY_CHARGING,
    BATTERY_CONNECTED_TO_CONDITIONER,
    BATTERY_CONDITIONING,
    BATTERY_TESTING
};

enum charger_state {
    CHARGER_IDLE = 0,
    CHARGER_CONNECTED, //Not currently used because chargers currently charge automatically once connected
    CHARGER_CHARGING
};

enum discharge_type {
    DISCHARGE_TEST_QUICK = 0,
    DISCHARGE_TEST_HIGH_RATE,
    DISCHARGE_CONDITION_PULSE,
    DISCHARGE_CONDITION_LOW
};

//The discharge rates of the conditioner
enum conditioner_state {
    CONDITIONER_IDLE = 0,
    CONDITIONER_CONNECTED,
    CONDITIONER_TEST_QUICK,
    CONDITIONER_TEST_HIGH_RATE,
    CONDITIONER_CONDITION_300W,
    CONDITIONER_CONDITION_200W,
    CONDITIONER_CONDITION_120W,
    CONDITIONER_CONDITION_100W,
    CONDITIONER_CONDITION_20W
};

//The various light bulbs from each bank
enum conditioner_load {
    CONDITIONER_LOAD_20W_0 = 0,
    CONDITIONER_LOAD_100W_0,
    CONDITIONER_LOAD_200W_0
};

//The list of possible load states
enum load_config {
    CONDITIONER_LOAD_CONFIG_NO_LOAD = 0,
    CONDITIONER_LOAD_CONFIG_20W,
    CONDITIONER_LOAD_CONFIG_100W,
    CONDITIONER_LOAD_CONFIG_120W,
    CONDITIONER_LOAD_CONFIG_200W,
    CONDITIONER_LOAD_CONFIG_300W
};

//The load config to use during a quick test
const load_config TEST_LOAD_CONFIG = CONDITIONER_LOAD_CONFIG_300W;

enum charger_event {
    EVENT_CHARGER_CONNECTED = 0,
    EVENT_CHARGER_DISCONNECTED,
    EVENT_CHARGER_CHARGING_START,
    EVENT_CHARGER_CHARGING_STOP
};

enum conditioner_event {
    EVENT_CONDITIONER_CONNECTED = 0,
    EVENT_CONDITIONER_DISCONNECTED,
    EVENT_CONDITIONER_START,
    EVENT_CONDITIONER_SWAP_STAGE,
    EVENT_CONDITIONER_END,
    EVENT_CONDITIONER_HALT,
    EVENT_BATTERY_TEST_HALT,
    EVENT_BATTERY_TEST_END
};

enum menu_state {
    MENU_HOME = 0,
    MENU_BATTERY_STATUS,
    MENU_PROMPT,
    MENU_QUEUE,
    MENU_RESULTS,
    MENU_DEBUG,
    MENU_CONDITION_TYPE
};

enum debug_menu_page {
    DEBUG_PAGE_BATTERIES = 0,
    DEBUG_PAGE_CHARGERS,
    DEBUG_PAGE_CONDITIONERS,
    DEBUG_PAGE_VOLTAGES,
    DEBUG_PAGE_CURRENTS,
    DEBUG_PAGE_MISC
};

enum results_menu_page {
    RESULTS_PAGE_WATTHOURS = 0,
    RESULTS_PAGE_TEST_QUICK,
    RESULTS_PAGE_TEST_HIGH_RATE
};

enum analog_read_filter_type {
    ANALOG_READ_FILTER_HIGHEST = 0,
    ANALOG_READ_FILTER_LOWEST,
    ANALOG_READ_FILTER_MEAN,
    ANALOG_READ_FILTER_RMS
};

enum display_type {
    DISPLAY_PRIMARY = 0,
    DISPLAY_SECONDARY
};

enum battery_quick_test_status {
    QUICK_TEST_STATUS_NOT_TESTED = 0,
    QUICK_TEST_STATUS_TESTING,
    QUICK_TEST_STATUS_PASS,
    QUICK_TEST_STATUS_NO_PASS,
    QUICK_TEST_STATUS_ERROR
};

#endif /* ENUMS_H_ */
